/*
 * 
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding GB2312
 */
package net.comm.message.service.impl;

import java.sql.Connection;
import java.util.List;

import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;
import net.comm.common.common;



import net.comm.message.bean.BaseMsgcont;
import net.comm.message.db.MsgcontServiceSqlMysql;
import net.comm.message.db.DbMsgcontService;
import net.comm.message.web.form.MsgcontForm;
import net.comm.param.service.impl.SequenceMgr;
import net.comm.util.DatetimeUtil;
import net.comm.util.DbcpPool;

import org.apache.log4j.Logger;
import org.sakaiproject.db.impl.BasicSqlService;


/**
 *
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class MsgcontServiceImpl {

	static Logger logger = Logger.getLogger(MsgcontServiceImpl.class.getName());

	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List list(MsgcontForm theForm,  ReturnMessage msg) 
	{
		List list = null;
		
		return list;
	}
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void get(MsgcontForm theForm, ReturnMessage msg) 
	{
		Connection cn = null;
		
		try
		{
			cn = DbcpPool.getConnection();
	
			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = theForm.getMsgID();

			MsgcontServiceSqlMysql servSql = new MsgcontServiceSqlMysql();
			
			String sql = servSql.getbykeySql();
			
			BasicSqlService  bSqlServ = new BasicSqlService();
						
			List list = bSqlServ.dbRead(cn, sql, fields, new DbMsgcontService());
			if(list.size() == 0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}			
			BaseMsgcont base = (BaseMsgcont)list.get(0);

			theForm.setMsgID(base.getMsgID());
			theForm.setMsg(base.getMsg());
			theForm.setMTime(base.getMTime());
		    
			DbcpPool.returnConnection(cn);			
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		}finally {
		
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}


	
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void get(Connection cn, MsgcontForm theForm, ReturnMessage msg) 
	{
		try
		{
			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = theForm.getMsgID();

			MsgcontServiceSqlMysql servSql = new MsgcontServiceSqlMysql();
			
			String sql = servSql.getbykeySql();
			
			BasicSqlService  bSqlServ = new BasicSqlService();
						
			List list = bSqlServ.dbRead(cn, sql, fields, new DbMsgcontService());
			if(list.size() == 0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}			
			BaseMsgcont base = (BaseMsgcont)list.get(0);

			theForm.setMsgID(base.getMsgID());
			theForm.setMsg(base.getMsg());
			theForm.setMTime(base.getMTime());
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		}
		
		return ;
	}


	/**
	 * 添加会员操作日志到表中
	 * @see net.IUserbasicService.module.other.IUserService#add(net.swift.common.ReturnMessage)
	 */
	public void add(Connection cn, MsgcontForm theForm, ReturnMessage msg) 
	{
		try
		{

			//
			Object[] fields = new Object[3];
			
			theForm.setMsgID(SequenceMgr.generateSeq(GlbFinal.SEQ_INITDATE, GlbFinal.SEQ_MSGCONT));
			
			fields[0] = theForm.getMsgID();
			fields[1] = theForm.getMsg();
			fields[2] = DatetimeUtil.getDatetime();
			
			MsgcontServiceSqlMysql servSql = new MsgcontServiceSqlMysql();
			String sql = servSql.getInsertSql();
			BasicSqlService  bSqlServ = new BasicSqlService();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 插入记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		}
		
		return ;
	}
	/**
	 * 删除一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delete(MsgcontForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);

			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = theForm.getMsgID();
			
			MsgcontServiceSqlMysql servSql = new MsgcontServiceSqlMysql();
			
			String sql = servSql.getDeleteSql();
			
			BasicSqlService  bSqlServ = new BasicSqlService();
								
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(res){
				common.setAppFailMsg(msg , " 删除记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}

	/**
	 * 更新一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void update(MsgcontForm theForm, ReturnMessage msg) 
	{
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);

			MsgcontServiceSqlMysql servSql = new MsgcontServiceSqlMysql();
			
			String sql = servSql.getbykeySql();
		
			BasicSqlService  bSqlServ = new BasicSqlService();

			//取原记录的信息			
			Object[] fields2 = new Object[1];

			fields2[0] = theForm.getMsgID();
			
			List list = null;
		
			list = bSqlServ.dbRead(cn, sql, fields2, new DbMsgcontService());
			if(list.size() == 0){
				
				common.setAppFailMsg(msg , " 记录不存在，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			//填充到theForm的空字段中
						
			BaseMsgcont base = (BaseMsgcont)list.get(0);
			

			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[3];
			
			fields[0] = theForm.getMsg();
			fields[1] = DatetimeUtil.getDatetime();
			fields[2] = theForm.getMsgID();
			
			sql = servSql.getUpdateSql();
			
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 更新记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);			
			DbcpPool.returnConnection(cn);
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
				
		return ;
	}

}
